#!/usr/bin/env python
#-*-coding:utf-8-*-
from  urllib import parse,request
import getproxy
from  time import sleep
import os
import re
print('*********************************')
print('*****                       *****')
print('*****     百度文库下载器      *****')
print('*****                       *****')
print('*********************************')
print('注意 必须为百度文库且不收费 ,下载文件名为文库下载 请及时改名或移动')
s=input('请输入网址 ：')
#ss=input('请输入后缀名(txt ? ppt ?..):')
#s='https://wenku.baidu.com/view/f2ff863731126edb6f1a10f8.html?from=search'
if('https' in s):
    s= 'http'+s[5:]
print(s)
url='http://wenkuquan.com/default.aspx'#接口
head = {}
head['User-Agent']=r'Mozilla/5.0 (Windows NT 6.1; Trident/7.0; rv:11.0) like Gecko'
head['Cookie']='rememberUser=true; username=zanzanzan; password=000999888'
head['Content-Type']='application/x-www-form-urlencoded'
date={'username':'zanzanzan','password':'000999888','txtUrl':s,'rememberUser':'on'}
data=parse.urlencode(date).encode('utf-8')


res=request.Request(url=url,headers=head,data=data)
iss=True
qs=()
s=""
while(iss):
    try:
        iss=False
        #print('1')
        ip = getproxy.get_()
        #print(ip)
        op = request.ProxyHandler({'http': ip})
        opnner = request.build_opener(op)
        realurl = opnner.open(res,timeout=3)
        s=realurl.read().decode('utf-8')
        #print(s)

        if  'http' not in s:
            k=10

            print('下载中',end=' ')
            while k<10:
                print('..',end='')
                sleep(1)
                k+=1
            print('              99 !!!下载错误')
            os._exit(-1)

    except:
        print('2')
        iss=True


if('https' in s):
    s= 'http'+s[5:]
qs=re.findall('(\.[a-z]{3,4})',s)
print(s)
print(qs)
if(r'http://w' in s):
   ss=qs[2]
else :
   ss=qs[0]
#print(ss)
file=request.urlopen(s,timeout=3).read()
with open('文库下载'+ss,'wb') as f:
    f.write(file)
print('成功!!')










